package com.leyou.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.utils.RedisConstant;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *  根据id查询品牌
     * @param id 品牌的id
     * @return 品牌对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {

        return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));

    }

    /**
     * 根据品牌id集合查询品牌集合
     * @param ids id集合
     * @return BrandDTO集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {

        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(ids)));

    }

    /**
     * 分页查询品牌
     * @param currentPage 当前页
     * @param pageSize 每页显示条数
     * @param wrapper 查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer currentPage,
            @RequestParam(value = "rows",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "key",required = false) String wrapper
    ){

        return ResponseEntity.ok(brandService.queryBrandByPage(currentPage,pageSize,wrapper));

    }

    /**
     * 根据分类id查询品牌
     * @param cid 分类id
     * @return 品牌集合
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long cid) {

        return ResponseEntity.ok(categoryBrandService.queryBrandByCategoryId(cid));

    }

    /**
     * 新增品牌
     * @param brandDTO 品牌对象
     * @return 操作结果状态信息
     */
    @PostMapping/*("/saveBrand")*/
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO) {

        brandService.saveBrand(brandDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    /**
     * 修改品牌
     * @param brandDTO 品牌及分类信息
     * @return 操作结果状态信息
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){

        brandService.updateBrand(brandDTO);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id) {

        brandService.deleteById(id);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    @GetMapping("/upload")
    public ResponseEntity<Void> upload(@RequestParam("fileName") String fileName) {

        System.out.println(fileName);

        fileName = "http://yxs-heima110-leyou.oss-cn-shanghai.aliyuncs.com/" + fileName;

        redisTemplate.opsForSet().add(RedisConstant.BRAND_PIC_RESOURCES,fileName);

        return ResponseEntity.status(HttpStatus.ACCEPTED).build();

    }

}
